package com.zj.album.graphy.views;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zj.album.R;


/**
 * Created by zhaojie on 2017/11/6.
 */

public class SmallLoadingView extends FrameLayout {

    private LinearLayout llLoading, llErro;
    private CallRefresh refresh;
    private View rootView;
    private TextView tvLoading, tvErro;
    private AlphaAnimation anm_en, anm_ex;

    public SmallLoadingView(@NonNull Context context) {
        this(context, null, 0);
    }

    public SmallLoadingView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SmallLoadingView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.photo_small_loading, null, false);
        addView(rootView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        llLoading = (LinearLayout) rootView.findViewById(R.id.small_llLoading);
        llErro = (LinearLayout) rootView.findViewById(R.id.small_llErro);
        tvLoading = (TextView) rootView.findViewById(R.id.small_tvLoading);
        tvErro = (TextView) rootView.findViewById(R.id.small_tvErro);
        anm_en = new AlphaAnimation(0.0f, 1.0f);
        anm_en.setDuration(350);
        anm_ex = new AlphaAnimation(1.0f, 0.0f);
        anm_ex.setDuration(400);
        llErro.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                loading("");
                if (refresh != null) refresh.onCallRefresh();
            }
        });
        loading("");
        setClickable(true);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
    }

    public void hide() {
        startAnimation(anm_ex);
        setVisibility(GONE);
    }

    public void loading(String s) {
        if (!TextUtils.isEmpty(s)) tvLoading.setText("" + s);
        if (getVisibility() == GONE) {
            startAnimation(anm_en);
            setVisibility(VISIBLE);
            if (llErro != null && llErro.getVisibility() == VISIBLE) {
                llErro.setVisibility(GONE);
            }
            if (llLoading != null) {
                llLoading.setVisibility(VISIBLE);
            }
        } else {
            if (llErro != null && llErro.getVisibility() == VISIBLE) {
                if (anm_ex != null) llErro.startAnimation(anm_ex);
                llErro.setVisibility(GONE);
            }
            if (llLoading != null) {
                if (anm_en != null) llLoading.startAnimation(anm_en);
                llLoading.setVisibility(VISIBLE);
            }
        }
    }

    public void erro(String s) {
        if (!TextUtils.isEmpty(s)) tvErro.setText("" + s);
        if (getVisibility() == GONE) {
            startAnimation(anm_en);
            setVisibility(VISIBLE);
            if (llLoading != null && llLoading.getVisibility() == VISIBLE) {
                llLoading.setVisibility(GONE);
            }
            if (llErro != null) {
                llErro.setVisibility(VISIBLE);
            }
        } else {
            if (llLoading != null && llLoading.getVisibility() == VISIBLE) {
                if (anm_ex != null) llLoading.startAnimation(anm_ex);
                llLoading.setVisibility(GONE);
            }
            if (llErro != null) {
                if (anm_en != null) llErro.startAnimation(anm_en);
                llErro.setVisibility(VISIBLE);
            }
        }
    }


    public void setCallInterface(CallRefresh refresh) {
        this.refresh = refresh;
    }

    public interface CallRefresh {
        void onCallRefresh();
    }
}
